package com.kdtech.analyse.NewsPaper;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kdtech.crawler.CrawlHTML;
import com.kdtech.entity.crawler.UrlMeta;
import com.kdtech.entity.data.NewsMeta;
import com.kdtech.utils.DateUtils;
import com.kdtech.utils.HtmlCleaner;
import com.kdtech.utils.StringUtils;
import com.kdtech.analyse.AnalyseNews;
/**
 * http://mzsb.yunnan.cn/html/2012-11/29/node_343.htm 民族时报
 * http://ccwb.yunnan.cn/html/2012-12/04/node_19.htm 春城晚报
 * @author allen 
 */
public class YunNnanNewsPaperAnalyse implements AnalyseNews {
	
	public boolean isDetailPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://mzsb.yunnan.cn/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm.*",
				"http://ccwb.yunnan.cn/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/content_.*.htm.*"
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}

	
	public NewsMeta parserHtml(UrlMeta urlMeta) {		
		NewsMeta newspaper = new NewsMeta();
				
		String html = urlMeta.getHtml();
		String url = urlMeta.getUrl();	
		
				
		newspaper.setUrl(url);		
		
		String title = null;
		String content = null;
		Long date = null;
		String dateStr = null;
		Document doc = Jsoup.parse(html);
	
		title =doc.select("td#tt div#bt").text();
		if(StringUtils.isBlank(title)){
			title =doc.select("td.title1").text();
		}
		title=title.replace("_民族时报_云南网", "");
		title=title.replace("春城晚报_云南网", "");
		date=DateUtils.matchDate(url);
		
		content =HtmlCleaner.getContentHtml(url,doc.select("div#ozoom"));
		
		
		newspaper.setTitle(StringUtils.trimSpace(title));
		newspaper.setContent(StringUtils.trimSpace(content));
		newspaper.setDate(date);

		return newspaper;
	}
	
	
	public boolean isTaskPage(String url) {
		boolean bRet = false;
		String[] regex = {
				"http://mzsb.yunnan.cn/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm"		,		
				"http://ccwb.yunnan.cn/html/[0-9]{4}-[0-9]{2}/[0-9]{2}/node_.*.htm"		,		
				};
		for (int i = 0; i < regex.length; i++) {
			if (url.matches(regex[i])) {
				return true;
			}
		}
		return bRet;
	}
	
	
	

	
	
}
